/**
 * @author 王琪 <wangqi@huizhaofang.com>
 * @since 2020-03-08
 */
package com.diploma.project.aiqiyi.service;

import com.diploma.project.aiqiyi.common.Constants;
import com.diploma.project.aiqiyi.common.enums.CommonOutputEnum;
import com.diploma.project.aiqiyi.dao.UserDao;
import com.diploma.project.aiqiyi.exception.BusinessException;
import com.diploma.project.aiqiyi.model.User;
import com.diploma.project.aiqiyi.utils.DateUtil;
import com.diploma.project.aiqiyi.utils.SHA256Utils;
import com.diploma.project.aiqiyi.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class UserService {

    private final static int LOGIN_TIMEOUT = 30;

    @Autowired
    private UserDao userDao;
    @Autowired
    private HttpServletRequest httpServletRequest;

    public List<User> queryAllUsers() {
        return userDao.selectAll();
    }

    public User createUser(Long mobile, String password, String nickname) {
        User user = findByMobile(mobile);
        if (user != null)
            throw new BusinessException(CommonOutputEnum._4042);

        user = new User();
        String uuid = UUID.randomUUID().toString().replace("-", "");
        user.setUuid(uuid);
        user.setMobile(mobile);
        user.setLoginPassword(SHA256Utils.getSHA256StrJava(password));
        user.setNickname(nickname);
        user.setVersion("1.0");
        user.setPlatform((byte) 1);
        userDao.insertSelective(user);

        Constants.cache.put(uuid, DateUtil.addMinute(new Date(), LOGIN_TIMEOUT));
        return user;
    }

    public User findByMobile(Long mobile) {
        User user = new User();
        user.setMobile(mobile);
        return userDao.selectOne(user);
        //return userDao.selectByMobile(mobile);
    }

    public User findByUUID(String uuid) {
        User user = new User();
        user.setUuid(uuid);
        return userDao.selectOne(user);
    }

    public User queryUserInfo() {
        return (User) httpServletRequest.getAttribute("user");
    }

    public void checkUser() {
        String token = httpServletRequest.getHeader("token");
        if (StringUtil.isBlank(token))
            throw new BusinessException(CommonOutputEnum._4043);

        Date date = Constants.cache.get(token);
        if (date == null || DateUtil.compareTime(new Date(), date) > 0)
            throw new BusinessException(CommonOutputEnum._4044);

        httpServletRequest.setAttribute("user", findByUUID(token));
    }

    public User login(Long mobile, String password) {
        User user = findByMobile(mobile);
        if (user == null)
            throw new BusinessException(CommonOutputEnum._4045);

        if (!SHA256Utils.getSHA256StrJava(password).equals(user.getLoginPassword()))
            throw new BusinessException(CommonOutputEnum._4046);

        Constants.cache.put(user.getUuid(), DateUtil.addMinute(new Date(), LOGIN_TIMEOUT));
        return user;
    }

    //修改密码
    public void updatepassword(String newPassword){
        User user = (User) httpServletRequest.getAttribute("user");
        if(user == null)
            throw new BusinessException(CommonOutputEnum._4045);

        if(SHA256Utils.getSHA256StrJava(newPassword).equals(user.getLoginPassword()))
          return;

        User where = new User();
        where.setId(user.getId());
        where.setLoginPassword(SHA256Utils.getSHA256StrJava(newPassword));
        userDao.updateByPrimaryKeySelective(where);
    }

    public void exitLogin(){
        User user = (User) httpServletRequest.getAttribute("user");
        Constants.cache.remove(user.getUuid());
    }
}
